<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit" label-width="120px">
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName" placeholder="请输入类型名称"></el-input>
      </el-form-item>
      <el-form-item label="类型别名" prop="typeAliases">
        <el-input v-model="dataForm.typeAliases" placeholder="请输入类型别名（注：字母）"></el-input>
      </el-form-item>
      <el-form-item label="页面标题" prop="seoTitle">
        <el-input v-model="dataForm.seoTitle" placeholder="请输入页面标题（注：城市动态加话括弧“{城市}”）"></el-input>
      </el-form-item>
      <el-form-item label="页面描述" prop="seoDesc">
        <el-input v-model="dataForm.seoDesc" placeholder="请输入页面描述（注：城市动态加话括弧“{城市}”）"></el-input>
      </el-form-item>
      <el-form-item label="页面关键字" prop="seoKeyword">
        <el-input v-model="dataForm.seoKeyword" placeholder="请输入页面关键字（注：城市动态加话括弧“{城市}”）"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="请输入排序（注：从大到小）"></el-input>
      </el-form-item>
      <el-form-item label="封面图" prop="imgUrl">
        <el-upload
          class="uploader"
          :action="uploaderUrl"
          :show-file-list="false"
          :on-success="handleUploaderSuccess"
          :before-upload="beforeUploaderUpload">
          <img v-if="dataForm.imgUrl" :src="dataForm.imgUrl" class="img" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <q class="notice">注：请上传 240px * 240px 图片</q>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :disabled="dataFormDisabled">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        uploaderUrl: this.$http.adornUrl('/api/v1/cms/file/uploadImg'),
        visible: false,
        dataFormDisabled: false,
        dataForm: {
          id: '',
          typeName: '',
          typeAliases: '',
          seoTitle: '',
          seoDesc: '',
          seoKeyword: '',
          imgUrl: '',
          sort: ''
        }
      }
    },
    methods: {
      init (obj) {
        this.dataForm = {
          id: '',
          typeName: '',
          typeAliases: '',
          seoTitle: '',
          seoDesc: '',
          seoKeyword: '',
          imgUrl: '',
          sort: ''
        }
        if (obj) {
          this.dataForm = {
            id: obj.id,
            typeName: obj.typeName,
            typeAliases: obj.typeAliases,
            seoTitle: obj.seoTitle,
            seoDesc: obj.seoDesc,
            seoKeyword: obj.seoKeyword,
            imgUrl: obj.imgUrl,
            sort: obj.sort
          }
        }
        this.visible = true
      },
      // 上传前
      beforeUploaderUpload (file) {
        if (file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message({
            message: '请上传图片文件',
            type: 'warning'
          })
          return false
        }
      },
      // 上传成功
      handleUploaderSuccess (res) {
        if (res.code !== 200) {
          this.$message({
            message: res.msg,
            type: 'error'
          })
          return false
        }
        this.dataForm.imgUrl = res.data
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataFormDisabled = true
            this.$http({
              url: this.$http.adornUrl(`/api/v1/cms/newsType/${this.dataForm.id ? 'updateNewsType' : 'addNewsType'}`),
              method: 'POST',
              data: this.$http.adornParams(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: data.msg,
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                    this.dataFormDisabled = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  .uploader .el-upload {width: 240px;height: 240px;border: 1px dashed #dcdfe6;display: flex;align-items: center;justify-content: center;border-radius: 4px;overflow: hidden;transition: .2s;}
  .uploader .el-upload:hover {border-color: #c0c4cc;transition: .2s;}
  .uploader .el-upload:focus {border-color: #17B3A3;transition: .2s;}
  .uploader .el-upload .img {width: 100%;height: 100%;background-color: #f1f4f5;}
  .uploader~.notice {font-size: 12px;color:#999;}
</style>